<?php

namespace adminmatrix\migration\command\migrate;

use adminmatrix\migration\command\Migrate;
use adminmatrix\migration\Migrator;
use think\console\input\Option as InputOption;
use think\console\Input;
use think\console\Output;
use Phinx\Db\Adapter\AdapterFactory;
use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface;
use Phinx\Util\Util;
class Status extends Migrate
{
    protected function configure()
    {
        $this->setName('migrate:status')
            ->addOption('app', '-a', InputOption::VALUE_OPTIONAL, 'app name')
            ->setDescription('查询数据库迁移状态')
            ->addOption('--format', '-f', \think\console\input\Option::VALUE_REQUIRED, 'The output format: text or json. Defaults to text.')
            ->setHelp(<<<EOT
The <info>migrate:status</info> command prints a list of all migrations, along with their current status

<info>php think migrate:status</info>
<info>php think migrate:status -f json</info>
EOT
            );
        $this->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The database connection to use.');
    }


    protected function execute(Input $input, Output $output)
    {
        $app = $this->appName = $input->getOption('app');
        $format = $input->getOption('format');

        if (null !== $format) {
            $output->writeln('<info>using format</info> ' . $format);
        }

        // print the status
        return $this->printStatus($format);
    }

    protected function printStatus($format = null)
    {
        $output     = $this->output;
        $migrations = [];

//        halt($this->getMigration());
        if (count($this->getMigrations())) {
            // TODO - rewrite using Symfony Table Helper as we already have this library
            // included and it will fix formatting issues (e.g drawing the lines)
            $output->writeln('');
            $output->writeln(' Status  Migration ID    Started              Finished             Migration Name  ');
            $output->writeln('------------------------------------------------------------------------------------');

            $versions      = $this->getVersionLog();
            $maxNameLength = $versions ? max(array_map(function ($version) {
                return strlen($version['migration_name']);
            }, $versions)) : 0;

            foreach ($this->getMigrations() as $migration) {
                $version = array_key_exists($migration->getVersion(), $versions) ? $versions[$migration->getVersion()] : false;
                if ($version) {
                    $status = '     <info>up</info> ';
                } else {
                    $status = '   <error>down</error> ';
                    $version = [];
                    $version['start_time'] = $version['end_time'] = $version['breakpoint'] = '';
                }
                $maxNameLength = max($maxNameLength, strlen($migration->getName()));

                $output->writeln(sprintf('%s %14.0f  %19s  %19s    <comment>%s</comment>', $status, $migration->getVersion(), $version['start_time'], $version['end_time'], $migration->getName() ));

                if ($version && $version['breakpoint']) {
                    $output->writeln('         <error>BREAKPOINT SET</error>');
                }

                $version['appname'] = $migration->getName();

                $migrations[] = [
                    'migration_status' => trim(strip_tags($status)),
                    'migration_id'     => sprintf('%14.0f', $migration->getVersion()),
                    'migration_name'   => $migration->getName(),
                    'application_name' => $this->appName,

                ];
                unset($versions[$migration->getVersion()]);
            }


            if (count($versions)) {
                foreach ($versions as $missing => $version) {
                    $output->writeln(sprintf('     
     <error>up</error>  %14.0f  %19s  %19s  <comment>%s</comment>  <error>** MISSING **</error>',
                        $missing,
                        $version['start_time'],
                        $version['end_time'],
                        str_pad($version['migration_name'], $maxNameLength,  ' '),
                        $version['appname']
                    ));

                    if ($version && $version['breakpoint']) {
                        $output->writeln('         <error>BREAKPOINT SET</error>');
                    }
                }
            }
        } else {
            // there are no migrations
            $output->writeln('');
            $output->writeln('There are no available migrations. Try creating one using the <info>create</info> command.');
        }

        // write an empty line
        $output->writeln('');
        if ($format !== null) {
            switch ($format) {
                case 'json':
                    $output->writeln(json_encode([
                        'pending_count' => count($this->getMigration()),
                        'migrations'    => $migrations
                    ]));
                    break;
                default:
                    $output->writeln('<info>Unsupported format: ' . $format . '</info>');
            }
        }
    }



}